package com.china08.yxyapi.api.manage.sch.platform.statistics.resource;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.statistics.UserUploadResourceTotal;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.mg.UserUploadResourceTotalService;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 数据统计学校 资源统计 数据详情
 * 
 */
@CrossOrigin
@RestController
public class ResourceDataDetailController extends YxyApiBaseController {
	@Autowired
	private UserUploadResourceTotalService userUploadResourceTotalService;

	@ApiOperation(value = "数据统计学校      资源统计     数据详情")
	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/sch/platform/statistics/resource/resourcedatadetail/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
			@RequestBody ReqModel req) {
		Pageable pageable = new PageRequest(req.page, req.size);
		RespModel model = new RespModel();
		String schoolId = getProfile(request, response).getSchoolId();
		Page<UserUploadResourceTotal> userUploadResourceTotalLst = userUploadResourceTotalService.getDataDetail(
				schoolId, req.resourceOrderList, req.shareResOrderList, req.weikeOrderList, req.shareWeikeOrderList,
				req.username, pageable);
		// 进行封装（每页数据不能超过10条）
		List<RespModel> modelLst = new ArrayList<RespModel>();
		Integer order = 0;
		// 便利查询到的最终数据
		for (UserUploadResourceTotal userUploadResourceTotal : userUploadResourceTotalLst) {
			model = new RespModel();
			order += 1;
			if (userUploadResourceTotal.getUserNick() != null) {
				model.userNike = userUploadResourceTotal.getUserNick();
			}
			if (userUploadResourceTotal.getResCount() != null) {
				model.resCount = userUploadResourceTotal.getResCount();
			}
			if (userUploadResourceTotal.getShareResCount() != null) {
				model.shareResCount = userUploadResourceTotal.getShareResCount();
			}
			if (userUploadResourceTotal.getWeikeCount() != null) {
				model.weikeCount = userUploadResourceTotal.getWeikeCount();
			}
			if (userUploadResourceTotal.getShareWeikeCount() != null) {
				model.shareWeikeCount = userUploadResourceTotal.getShareWeikeCount();
			}
			model.order = order;
			modelLst.add(model);
		}
		// 进行封装（每页数据不能超过10条）
		/*List<RespModel> schoolUploadResourceReportLst2 = new ArrayList<RespModel>();
		if (modelLst.size() > 10) {
			// 进行封装（每页数据不能超过10条）
			schoolUploadResourceReportLst2 = modelLst.subList((pageable.getPageNumber() - 1) * pageable.getPageSize(),
					10);
		} else {
			schoolUploadResourceReportLst2 = modelLst;
		}*/
		PageImpl<RespModel> pageImpl = new PageImpl<RespModel>(modelLst, pageable,
				userUploadResourceTotalLst.getTotalElements());
		Page<RespModel> pageLst = pageImpl;
		return pageLst;
	}

	protected static class RespModel extends YxyApiRespModel {
		// 序号
		public Integer order;
		// 用户名
		public String userNike;
		// 上传资源
		public Long resCount;
		// 共享资源
		public Long shareResCount;
		// 上传微课
		public Long weikeCount;
		// 共享微课
		public Long shareWeikeCount;
	}

	protected static class ReqModel extends YxyApiPageReqModel {
		// 上传资源列表排序
		public String resourceOrderList;
		// 共享资源列表排序
		public String shareResOrderList;
		// 上传微课
		public String weikeOrderList;
		// 共享微课
		public String shareWeikeOrderList;
		// 用户名模糊查询
		public String username;
	}
}
